/*
 * Shooter - a weapon controlled by FortressData.
 *
 * Copyright 2008 James W. Hofmann
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

package rules;

class Shooter
{

	public var x : Int;
	public var y : Int;
	public var parent : FortressData;
	public var shoottime : Int;

	public var target : Array<GameTile>;
	public var validtargets : Array<GameTile>;

	public var level : Int;
	public var dmg : Int;
	public var experience : Int;

	inline public static var reloadtime = 10;
	inline public static var basedamage = 50;
	inline public static var addeddamage = 9; 
	inline public static var expfornextlevel = 11;
	inline public static var targetsize = 2;
	// because addeddamage is less than the creep hp gain each wave
	// and expfornextlevel requires more kills than exist in a wave,
	// the player has to compensate by concentrating firepower around
	// the map. This is the key to making the strategy interesting.

	public function new(parent : FortressData)
	{
		this.parent = parent;
		this.x = parent.x;
		this.y = parent.y;
		this.target = gentarget(this.x,this.y,targetsize);
		this.validtargets = gentarget(this.x,this.y,targetsize);
		this.shoottime = 0;
		this.level = 1;
		this.dmg = basedamage;
		this.experience = 0;
	}

	public function addexperience()
	{
		this.experience+=1;
		if (experience>=expfornextlevel)
		{
			upgrade();
			experience = 0;
		}
	}

	public function upgrade()
	{
		
		this.level+=1;
		this.dmg+=addeddamage;
		
		GameResponse.addcomposition(
			new GameResponse(GameResponse.msg_shooterupgrade,this)
		);
		
	}
	
	public function downgrade()
	{
		
		if(level>1)
		{
			this.level-=1;
			this.dmg-=addeddamage;

			GameResponse.addcomposition(
			new GameResponse(GameResponse.msg_shooterdowngrade,this)
			);
		
		}

	}

	public function selecttarget(x : Int, y : Int, dryrun : Bool)
	{

		if (!Lambda.exists(this.validtargets, function (n : GameTile)
		{return (n.x==x && n.y==y);}))
		{
			throw new GameResponse(GameResponse.msg_error,
			"Not a valid target");
		}

		if (dryrun)
		{
			GameResponse.addcomposition(
				new GameResponse(GameResponse.msg_dryconfirm,null));
		}
		else
		{
				
			this.target = gentarget(x,y,targetsize);
				
			GameResponse.addcomposition(
			new GameResponse(GameResponse.msg_towertargetchange,this));
		}			
	}

	public function gentarget(centerx : Int, centery : Int, size : Int)
	: Array<GameTile>
	{
		// generate a "circular" target
		// this is an inefficient algorithm for large sizes,
		// it "grows" the target by testing the neighbors of 
		// the existing tiles, even the ones surrounded
		 		
		var newtarg : Array<GameTile> = new Array();
		newtarg.push(RulesAPI.g.m.tiles[centerx][centery]);
		
		
		for (s in 0...size)
		{
			var iterlist : List<GameTile> = new List();
			for (cur in newtarg)
			{
				for (n in cur.neighbors)
				{
					iterlist.push(n);
				}
			}
			iterlist.map(function(t : GameTile)
				{ if (!Lambda.has(newtarg, t))
					newtarg.push(t);
				}
			);
		}
		
		return newtarg;
	}

	public function up()
	{
		shoottime -=1;
		if (shoottime<=0)
		{
			if (shoot())
				shoottime = reloadtime;
			else
				shoottime = 0;
		}
	}

	public function shoot() : Bool
	{
		// shoot if I see something in my target area.
		// otherwise wait another frame.

		var targets = Lambda.filter(target, function(z : GameTile)
		{if (z.creeps.length>0) return true; return false;});
		if (targets.length==0)
			return false;
		else
		{
			// when I pick the first target I actually take the closest 
			// one to the center,
			// because of how the target list is generated.
			var newshot = new Shot(x,y,targets.first().x,
								   targets.first().y,0.33,dmg);
			this.addexperience();
			GameResponse.addcomposition(
				new GameResponse(GameResponse.msg_shooterfired, [this,
				newshot]));
			return true;
		}
	}

}
